﻿using System;
using System.Linq;
using Ewk.MachineLearning.Persistance.Domain;
using Ewk.MachineLearning.Persistance.Interfaces;

namespace Ewk.MachineLearning.Persistance.DataAccess
{
    public class TextEntityRepository<T> : EntityRepository<T> 
        where T : TextEntity, new()
    {
        public TextEntityRepository(ICatalog catalog)
            :base(catalog)
        {
        }

        public T GetOrCreateEntity(string content)
        {
            content = content.ToUpperInvariant();

            return Catalog.Entities<T>()
                          .SingleOrDefault(w => w.Content == content)
                   ??
                   Add(new T
                       {
                           Content = content
                       });
        }

        public string GetContentById(Guid id)
        {
            return Catalog.Entities<T>()
                .Where(entity => entity.Id == id)
                .Select(arg => arg.Content)
                .SingleOrDefault();
        }
    }
}